﻿using System;

namespace FCS.Property
{
    /// <summary>
    /// 推荐的可视化比例 PnD
    /// </summary>
    public struct SuggestedVisualizationScale
    {
        public SuggestedVisualizationScaleType Type { get; set; }
        public double Min { get; set; }
        public double Max { get; set; }

        public SuggestedVisualizationScale(SuggestedVisualizationScaleType type, double min, double max)
        {
            this.Type = type;
            this.Min = min;
            this.Max = max;
        }

        public SuggestedVisualizationScale(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                this.Type = SuggestedVisualizationScaleType.Unknown;
                this.Min = 0d;
                this.Max = 100000d;
                return;
            }
            var spirts = str.Split(',');
            if (spirts.Length != 3)
            {
                this.Type = SuggestedVisualizationScaleType.Unknown;
                this.Min = 0d;
                this.Max = 100000d;
                return;
            }
            switch (spirts[0].ToUpper())
            {
                case "LINEAR":
                    this.Type = SuggestedVisualizationScaleType.Linear;
                    if (double.TryParse(spirts[1], out double lf1)) this.Min = lf1;
                    else this.Min = 0d;
                    if (double.TryParse(spirts[2], out double lf2)) this.Max = lf2;
                    else this.Max = 100000d;
                    break;
                case "LOGARITHMIC":
                    this.Type = SuggestedVisualizationScaleType.Logarithmic;
                    if (double.TryParse(spirts[2], out double gf1)) this.Min = gf1 == 0d ? 1d : gf1;
                    else this.Min = 1d;
                    if (double.TryParse(spirts[1], out double gf2)) this.Max = Math.Pow(10, gf2) * this.Min;
                    else this.Max = 100000d;
                    break;
                default:
                    this.Type = SuggestedVisualizationScaleType.Unknown;
                    this.Min = 0d;
                    this.Max = 100000d;
                    break;
            }
        }

        public override string ToString()
        {
            switch (Type)
            {
                case SuggestedVisualizationScaleType.Linear:
                    return string.Concat("LINEAR", ",", Min, ",", Max);
                case SuggestedVisualizationScaleType.Logarithmic:
                    return string.Concat("LOGARITHMIC", ",", Math.Log10(Max / (Min == 0d ? 1d : Min)), ",", (Min == 0d ? 1d : Min));
                default:
                    return string.Empty;
            }
        }

    }

    public enum SuggestedVisualizationScaleType
    {
        Unknown = 0,
        Linear = 1,
        Logarithmic = 2
    }
}
